package de.koller.pulsar;

import de.koller.aframe.util.resource.IOLoader;
import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.tree.Sea3X;
import de.koller.pulsar.scene.IScene;
import java.awt.image.BufferedImage;
import java.io.File;

public class PRoot extends Sea3X {
	
	private BufferedImage sceneFrame = null;
	private int framenumber = 0;
	private BufferedImage movieFrame = null;
	public final IScene scene;
	
	public static void renderScene( IScene scene ) {
		PRoot r = new PRoot( scene );
		r.initalize();
		
		while( r.sceneFrame == null ) {
			r.updating( 1 );
			r.rendering( 1 );
			
			if( r.movieFrame != null ) {
				new File( "./temp/pulsar_motion/" ).mkdirs();
				IOLoader.savePNG( r.movieFrame, "./temp//pulsar_motion/" + r.framenumber + ".png" );
				System.out.println( "motionframe: " + r.framenumber );
				r.framenumber++;
				r.movieFrame = null;
			}
		}
		
		r.releasing();
		
		IOLoader.savePNG( r.sceneFrame, "./temp/pulsar_scene.png" );
		ImagePreview.showImage( r.sceneFrame, r.sceneFrame.getWidth(), r.sceneFrame.getHeight() );
	}
	
	private PRoot( IScene scene ) {
		this.scene = scene;
	}

	@Override
	protected void init() {
		super.init();
		
		this.addChild( scene );
	}
	
	public void setSceneFrame( BufferedImage sceneFrame ) {
		this.sceneFrame = sceneFrame;
	}

	public int setMovieFrame( BufferedImage movieFrame ) {
		this.movieFrame = movieFrame;
		return framenumber;
	}
	
}
